package com.ruoyi.workstation.controller;

import java.util.List;

import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.workstation.domain.User;
import com.ruoyi.workstation.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workstation.domain.UserLog;
import com.ruoyi.workstation.service.IUserLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户日志Controller
 * 
 * @author ruoyi
 * @date 2024-01-13
 */
@Controller
@RequestMapping("/workstation/userlog")
public class UserLogController extends BaseController
{
    private String prefix = "workstation/userlog";
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IUserLogService userLogService;
    @Autowired
    private IUserService userService;
    @RequiresPermissions("workstation:userlog:view")
    @GetMapping()
    public String userlog()
    {
        return prefix + "/userlog";
    }

    /**
     * 查询客户日志列表
     */
    @RequiresPermissions("workstation:userlog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserLog userLog)
    {
        startPage();
        List<UserLog> list = userLogService.selectUserLogList(userLog);
        list.stream().parallel().forEach(user1 -> {
            if (user1.getCreator() != null) {
                User uu = userService.selectUserByUserId(user1.getCreator());
                user1.setCreateBy(uu.getUsername());
            }
            user1.setFileName(serverConfig.getUrl() + user1.getFileName());
        });
        return getDataTable(list);
    }

    /**
     * 导出客户日志列表
     */
    @RequiresPermissions("workstation:userlog:export")
    @Log(title = "客户日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserLog userLog)
    {
        List<UserLog> list = userLogService.selectUserLogList(userLog);
        ExcelUtil<UserLog> util = new ExcelUtil<UserLog>(UserLog.class);
        return util.exportExcel(list, "客户日志数据");
    }

    /**
     * 新增客户日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户日志
     */
    @RequiresPermissions("workstation:userlog:add")
    @Log(title = "客户日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserLog userLog)
    {
        return toAjax(userLogService.insertUserLog(userLog));
    }

    /**
     * 修改客户日志
     */
    @RequiresPermissions("workstation:userlog:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        UserLog userLog = userLogService.selectUserLogById(id);
        mmap.put("userLog", userLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户日志
     */
    @RequiresPermissions("workstation:userlog:edit")
    @Log(title = "客户日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserLog userLog)
    {
        return toAjax(userLogService.updateUserLog(userLog));
    }

    /**
     * 删除客户日志
     */
    @RequiresPermissions("workstation:userlog:remove")
    @Log(title = "客户日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userLogService.deleteUserLogByIds(ids));
    }
}
